package com.dynomedia.esearch.util.groupkeycache;

import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * 属于某一个GroupKey对象的Topkey对象。记录关键字及关键字的点击次数。
 * @author Administrator
 * @version 1.0
 * @created 19-十月-2009 16:50:13
 */
public class Topkey implements Comparable<Topkey> {

	private String key;
	private AtomicInteger count = new AtomicInteger(0);
	
	private static final Log logger = LogFactory.getLog(Topkey.class);

	public Topkey(){

	}

	public void finalize() throws Throwable {

	}

	/**
	 * 
	 * @param key
	 * @param count
	 */
	public Topkey(String key, Integer count){
		this.count.set(count);
		this.key = key;
	}

	public Integer getCount(){
		return count.get();
	}

	public String getKey(){
		return key;
	}

	/**
	 * 更新计数
	 * 
	 * @param count
	 */
	public void setCount(Integer count){
		this.count.set(count);
	}
	
	public Integer updateCount(){
		return this.count.incrementAndGet();		
	}
	
	/**
	 * 更新计数
	 * baipeng wrote at 下午02:08:07 2009-10-20
	 * @param count
	 * @return
	 */
	public Integer updateCount(Integer count){
		return this.count.addAndGet(count);		
	}

	/**
	 * 
	 * @param key
	 */
	public void setKey(String key){
		this.key = key;
	}

	/**
	 * 
	 * @param topkey
	 */
	public int compareTo(Topkey topkey){
		if(count.get() >= topkey.getCount()){
			return -1;
		}
		return 1;
	}

	public int hashCode(){
		return this.key.hashCode();
	}

	/**
	 * 
	 * @param anObject
	 */
	public boolean equals(Object anObject){
		if (anObject instanceof Topkey) {
			Topkey anTopkey = (Topkey) anObject;
			return (this.key).equals(anTopkey.getKey());
		}
		return false;
	}
	
	public String toString(){
		return "[key]:"+key+" [count]:"+count.get();
	}

}